package com.vrv.controller.permission;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.AdminInfo;
import com.vrv.entity.Form;
import com.vrv.entity.FormObj;
import com.vrv.entity.UserInfo;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.MD5Util;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/manager")
public class ManagerController {

    @RequestMapping("/list")
    public String list() {
        return "/permission/manager_list";
    }

    @RequestMapping("/toEdit")
    public String toAdd() {
        return "/permission/manager_edit";
    }

    @RequestMapping("/editUser")
    public String editUser(HttpServletRequest request, Integer id) {
        request.setAttribute("id", id);
        return "/permission/manager_editUser";
    }

    @RequestMapping(value = "getUserList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getUserList(HttpServletResponse response, Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        if (form.getUserType() == 0) {
            paramsCell.put("name", "userinfo");
        } else {
            paramsCell.put("name", "admininfo");
        }
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);

        Integer currentPage = form.getCurrentPage() == null ? 1 : form.getCurrentPage();
        Integer pageCount = form.getPageCount() == null ? 10 : form.getPageCount();
        tableInfoMap.put("from", (currentPage - 1) * 10);
        tableInfoMap.put("count", pageCount);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "name");
        paramsCell2.put("asname", "");
        mapList2.add(paramsCell2);
        Map<String, Object> paramsCell3 = new HashMap<String, Object>();
        paramsCell3.put("name", "account");
        paramsCell3.put("asname", "");
        mapList2.add(paramsCell3);
        Map<String, Object> paramsCell4 = new HashMap<String, Object>();
        paramsCell4.put("name", "id");
        paramsCell4.put("asname", "");
        mapList2.add(paramsCell4);
        Map<String, Object> paramsCell5 = new HashMap<String, Object>();
        paramsCell5.put("name", "addtime");
        paramsCell5.put("asname", "");
        mapList2.add(paramsCell5);
        Map<String, Object> paramsCell6 = new HashMap<String, Object>();
        paramsCell6.put("name", "type");
        paramsCell6.put("asname", "");
        mapList2.add(paramsCell6);
        tableInfoMap.put("cols", mapList2);

        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        if (form.getSysRole() != null && !"".equals(form.getSysRole())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("type", form.getSysRole());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }
        if (form.getAccount() != null && !"".equals(form.getAccount())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("account", "%%" + form.getAccount() + "%%");
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("like", paramsObj7);
            mapList3.add(paramsObj8);
        }
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);
        if ((form.getSysRole() != null && !"".equals(form.getSysRole()))
                || (form.getAccount() != null && !"".equals(form.getAccount()))) {
            tableInfoMap.put("where", paramsObj6);
        }

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        System.out.println(json);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getUserListCount", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getUserListCount(HttpServletResponse response, Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        if (form.getUserType() == 0) {
            paramsCell.put("name", "userinfo");
        } else {
            paramsCell.put("name", "admininfo");
        }
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);
        Integer currentPage = form.getCurrentPage() == null ? 1 : form.getCurrentPage();
        Integer pageCount = form.getPageCount() == null ? 10 : form.getPageCount();
        tableInfoMap.put("from", (currentPage - 1) * 10);
        tableInfoMap.put("count", pageCount);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "count(*)");
        paramsCell2.put("asname", "count");
        mapList2.add(paramsCell2);
        tableInfoMap.put("cols", mapList2);
        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        if (form.getSysRole() != null && !"".equals(form.getSysRole())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("type", form.getSysRole());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }
        if (form.getAccount() != null && !"".equals(form.getAccount())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("account", "%%" + form.getAccount() + "%%");
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("like", paramsObj7);
            mapList3.add(paramsObj8);
        }
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);
        if ((form.getSysRole() != null && !"".equals(form.getSysRole()))
                || (form.getAccount() != null && !"".equals(form.getAccount()))) {
            tableInfoMap.put("where", paramsObj6);
        }

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        System.out.println(json);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getNodes", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getNodes(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add("spark");
        params.put("target", XMLRead.AIM);
        params.put("option", "getnodes");
        params.put("services", list);
        params.put("object", "state");

        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "delUser", produces = "application/json;charset=UTF-8")
    public @ResponseBody String delUser(HttpServletRequest request, Integer id, Integer type,
            String account) {
        HttpSession session = request.getSession();
        String flag = "";
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add("spark");
        params.put("target", XMLRead.AIM);
        params.put("option", "delete_table");
        params.put("object", "table");

        Map<String, Object> paramsObj = new HashMap<String, Object>();
        if (type == 0) {
            paramsObj.put("name", "userinfo");
        } else {
            paramsObj.put("name", "admininfo");
        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(paramsObj);
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("tables", mapList);

        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("id", id);
        Map<String, Object> paramsObj4 = new HashMap<String, Object>();
        paramsObj4.put("term", paramsObj3);
        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        mapList2.add(paramsObj4);
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList2);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);
        // Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj2.put("where", paramsObj6);
        params.put("tableinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);

        if (str.indexOf("true") > 0) {
            FormObj formObj = new FormObj();
            if (session.getAttribute("type") != null) {
                if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                    AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(1);
                } else {
                    UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(0);
                }
                formObj.setUser(account);
                addLog(formObj, "删除用户");
            }
            flag = str;
        }
        return flag;
    }

    @RequestMapping(value = "addUser", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addUser(HttpServletRequest request, HttpServletResponse response,
            FormObj formObj) {
        HttpSession session = request.getSession();
        String flag = null;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "insert_table");
        params.put("object", "table");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        if (formObj.getType().equals("0")) {
            paramsObj.put("name", "userinfo");
        } else {
            paramsObj.put("name", "admininfo");
        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(paramsObj);
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("tables", mapList);

        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("colname", "account");
        paramsObj3.put("colvalue", formObj.getUser());
        Map<String, Object> paramsObj4 = new HashMap<String, Object>();
        paramsObj4.put("colname", "password");
        paramsObj4.put("colvalue", MD5Util.MD5(formObj.getPassword()));
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("colname", "type");
        paramsObj5.put("colvalue", formObj.getType());
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("colname", "perms");
        if (formObj.getType().equals("0")) {
            paramsObj6.put("colvalue", getPerms(formObj));
        } else {
            paramsObj6.put("colvalue", "");
        }
        Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj7.put("colname", "name");
        paramsObj7.put("colvalue", formObj.getUser());
        Map<String, Object> paramsObj8 = new HashMap<String, Object>();
        paramsObj8.put("colname", "deleted");
        paramsObj8.put("colvalue", 0);
        Map<String, Object> paramsObj9 = new HashMap<String, Object>();
        paramsObj9.put("colname", "addtime");
        paramsObj9.put("colvalue", sdf.format(new Date()));
        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        mapList2.add(paramsObj3);
        mapList2.add(paramsObj4);
        mapList2.add(paramsObj5);
        mapList2.add(paramsObj6);
        mapList2.add(paramsObj7);
        mapList2.add(paramsObj8);
        mapList2.add(paramsObj9);

        // Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj2.put("setvalues", mapList2);
        params.put("tableinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        if (str.indexOf("true") > 0) {
            if (session.getAttribute("type") != null) {
                if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                    AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(1);
                } else {
                    UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(0);
                }
                addLog(formObj, "增加用户");
            }
            flag = str;
        }
        return flag;
    }

    @RequestMapping(value = "editUser", produces = "application/json;charset=UTF-8")
    public @ResponseBody String editUser(HttpServletRequest request, HttpServletResponse response,
            FormObj formObj) {
        String flag = "";
        Map<String, Object> params = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        params.put("target", XMLRead.AIM);
        params.put("option", "update_table");
        params.put("object", "table");
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        if (formObj.getLastType() == 0) {
            paramsObj.put("name", "userinfo");
        } else {
            paramsObj.put("name", "admininfo");
        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(paramsObj);
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("tables", mapList);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("colname", "account");
        paramsObj3.put("colvalue", formObj.getUser());
        if (!MD5Util.MD5(formObj.getPassword()).equals(formObj.getEdpassword())) {
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("colname", "password");
            paramsObj4.put("colvalue", MD5Util.MD5(formObj.getPassword()));
            mapList2.add(paramsObj4);
        }
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("colname", "type");
        paramsObj5.put("colvalue", formObj.getType());
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("colname", "perms");
        paramsObj6.put("colvalue", getPerms(formObj));
        Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj7.put("colname", "name");
        paramsObj7.put("colvalue", formObj.getUser());

        mapList2.add(paramsObj3);
        mapList2.add(paramsObj5);
        mapList2.add(paramsObj6);
        mapList2.add(paramsObj7);
        paramsObj2.put("setvalues", mapList2);

        Map<String, Object> paramsObj8 = new HashMap<String, Object>();
        paramsObj8.put("id", formObj.getId());
        Map<String, Object> paramsObj9 = new HashMap<String, Object>();
        paramsObj9.put("term", paramsObj8);
        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        mapList3.add(paramsObj9);
        Map<String, Object> paramsObj10 = new HashMap<String, Object>();
        paramsObj10.put("must", mapList3);
        Map<String, Object> paramsObj11 = new HashMap<String, Object>();
        paramsObj11.put("bool", paramsObj10);
        // Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj2.put("where", paramsObj11);
        params.put("tableinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);

        if (str.indexOf("true") > 0) {
            if (session.getAttribute("type") != null) {
                if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                    AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(1);
                } else {
                    UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                    formObj.setAccount(userInfo.getAccount());
                    formObj.setIsAdmin(0);
                }
                addLog(formObj, "修改用户");
            }
            flag = str;
        }
        return flag;
    }

    public void addLog(FormObj formObj, String operationContent) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "insert_table");
        params.put("object", "table");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        paramsObj.put("name", "loginfo");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        mapList.add(paramsObj);
        Map<String, Object> paramsObj2 = new HashMap<String, Object>();
        paramsObj2.put("tables", mapList);

        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("colname", "module");
        paramsObj3.put("colvalue", operationContent);
        Map<String, Object> paramsObj4 = new HashMap<String, Object>();
        paramsObj4.put("colname", "operate");
        paramsObj4.put("colvalue", operationContent);
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("colname", "grade");
        paramsObj5.put("colvalue", 1);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("colname", "account");
        paramsObj6.put("colvalue", formObj.getAccount());
        Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj7.put("colname", "content");
        paramsObj7.put("colvalue", operationContent + formObj.getUser());
        Map<String, Object> paramsObj8 = new HashMap<String, Object>();
        paramsObj8.put("colname", "isadmin");
        paramsObj8.put("colvalue", formObj.getIsAdmin());
        Map<String, Object> paramsObj9 = new HashMap<String, Object>();
        paramsObj9.put("colname", "addtime");
        paramsObj9.put("colvalue", sdf.format(new Date()));
        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        mapList2.add(paramsObj3);
        mapList2.add(paramsObj4);
        mapList2.add(paramsObj5);
        mapList2.add(paramsObj6);
        mapList2.add(paramsObj7);
        mapList2.add(paramsObj8);
        mapList2.add(paramsObj9);

        // Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj2.put("setvalues", mapList2);
        params.put("tableinfo", paramsObj2);
        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
    }

    public String getPerms(FormObj formObj) {
        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("limitFlow", formObj.getLimitFlow());
        mapObj.put("limitNum", formObj.getLimitNum());
        mapObj.put("limitSpace", formObj.getLimitSpace());
        mapObj.put("limitNode", formObj.getLimitNode());
        JSONObject json = JSONObject.fromObject(mapObj);
        return json.toString();
    }

    @RequestMapping(value = "getUserById", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getUserById(HttpServletResponse response, Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        paramsCell.put("name", "userinfo");
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);

        tableInfoMap.put("from", 0);
        tableInfoMap.put("count", 1);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "*");
        paramsCell2.put("asname", "");
        mapList2.add(paramsCell2);
        // Map<String, Object> paramsCell3 = new HashMap<String, Object>();
        // paramsCell3.put("name", "account");
        // paramsCell3.put("asname", "");
        // mapList2.add(paramsCell3);
        // Map<String, Object> paramsCell4 = new HashMap<String, Object>();
        // paramsCell4.put("name", "id");
        // paramsCell4.put("asname", "");
        // mapList2.add(paramsCell4);
        // Map<String, Object> paramsCell5= new HashMap<String, Object>();
        // paramsCell5.put("name", "addtime");
        // paramsCell5.put("asname", "");
        // mapList2.add(paramsCell5);
        tableInfoMap.put("cols", mapList2);

        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("id", id);
        Map<String, Object> paramsObj4 = new HashMap<String, Object>();
        paramsObj4.put("term", paramsObj3);
        mapList3.add(paramsObj4);

        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);

        tableInfoMap.put("where", paramsObj6);

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        System.out.println(json);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }
}
